<?php
namespace Admin\Controller;
use Think\Controller;
class LunboController extends CommonController {
	public function _initialize(){
		parent::_initialize();
	}
	
    public function add(){
		$id=I('get.id');
		$result=false;
		if($id!=''){
			$db=M('User03');
			$result=$db->where("id={$id}")->find();
		}
		$dbs=M('Type');
		$res=$dbs->select();
		$this->assign('id',$id);
		$this->assign('result',$result);
		$this->assign('res',$res);
		$this->display();
    }
    public function table(){
		$keyword = I('get.keyword','','htmlspecialchars');
		$where = NULL;
		if( $keyword != '' ){
			$where = "title like '%{$keyword}%'";
		}
		
		
		//var_dump($rest);exit;
		
		$id = I('get.id');
		$act = I('get.act');
		$db = M('User03'); // 实例化User对象
		if( $act == 't_del' ){
			if( $id != '' ){
				$db->where("id={$id}")->delete();
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
		}
		
		
		
		$count = $db->where($where)->count();// 查询满足要求的总记录数
		$Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show  = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		
		$lunbo = M('Lunbo');
		$list = $lunbo->field('article_id')->select();
		$rest = array();
		foreach( $list as $val){
				$rest[]	= $val['article_id'];
				$res = $db->where("id={$val['article_id']}")->find();
				$data[] = array(
					'title' =>	$res['title'],
					'type'	=>	$res['type'],
					'img_src'	=>	$res['img_src'],
					'author'	=>	$res['author'],
					'click'	=>	$res['click']
				);
		}
		//var_dump($data);exit;
		
		//$data = $db->where("id=")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		
		$this->assign('data',$data);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板	
    }
    
    public function insert(){
		$id=I('get.id');
		$name=I('post.name');
		
		$dbs=M('Type');
		$ret=$dbs->where(array('name'=>$name))->find();
		$type_num=$ret['type_num'];
		
		$db=D("User03");
		$content=I('post.content', '', '');
		$data=$db->create();
		if(!$data){
			$this->error($db->getError());
		}else{
			$data['content']=$content;
			$data['type']=$name;
			$data['type_num']=$type_num;
			$data['addtime']=time();
			if($_FILES['img_src']['name']!=''){
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize=2*1024*1024 ;// 设置附件上传大小
				$upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$path=realpath(__ROOT__);
				$upload->rootPath=$path.'/Public/uploadfiles/'; // 设置附件上传根目录(要用绝对路径)
				$upload->savePath=''; // 设置附件上传（子）目录
				// 上传文件 
				$info=$upload->upload();
				if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				}else{// 上传成功
					//$this->success('上传成功！');
					$data['img_src']="/Public/uploadfiles/{$info['img_src']['savepath']}{$info['img_src']['savename']}";
				}
			}
			if($id==''){
				$db->add($data);
				$this->success('博文添加成功');
			}else{
				$db->data($data)->where("id={$id}")->save();
				$this->success('博文修改成功');
			}
		}
		
	}
}
